#!/usr/bin/env python
#
#       generatedefinitions.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import wx
from wx.lib.wordwrap import wordwrap

import accessories
import pytestgenerator

class GenerateEvents:

    def __init__(self,BaseFrame):
        self.Inst=BaseFrame

    def generatecase(self,event):
        """
        Event for Test Case generation
        """
        modname = self.Inst.modname.GetValue()
        selected_list=[]
        if not modname:
            accessories.infobox(self.Inst,"Unable to Resolve Package Name",\
                "PyTestGenerator Warning",wx.ICON_WARNING)
        else :
            selected_list=self._getselected()

        if selected_list :
            self._generate(selected_list,modname)
        elif not selected_list and len(self._getavailable()) >= 1:
            seleceted_list=self._getavailable()
            self._generate(selected_list,modname)
        
        if os.path.isfile("pytestgen_%s.py"%modname) and selected_list:
            accessories.infobox\
                (self.Inst,"Generated TestCase Path : %s"\
                %os.path.abspath("pytestgen_%s.py"%modname),\
                "Generated Testcase",wx.ICON_INFORMATION)

    def _generate(self,selectedList,modName=None):
        """
        Internal handling for generate event
        """
        pyframe = pytestgenerator.generatepyunit.PyFramework()
        fnlist=[]
        clist=[]
        methlist=[]

        pyframe.writefile("import %s\n\n"%modName)
        pyframe.writeheader()
        pyframe.writefile("%s module'''\n"%modName)

        pytestgenerator.TestGenerator().casegen(selectedList,modName,pyframe)

        pyframe.writefooter()
        pyframe.closefile(fname="pytestgen_%s.py"%modName)

    def _getselected(self):

        selected_list=[]

        for item in range(self.Inst.selc.GetItemCount()):
            subent=[]
            if str(self.Inst.avail.GetItem\
                (self.Inst.avail.FindItem(0,self.Inst.selc.GetItemText\
                (item)), 3).GetText()) == 'METHOD':
                subent.append(str(self.Inst.avail.GetItem\
                    (self.Inst.avail.FindItem(0,self.Inst.selc.GetItemText\
                    (item)), 2).GetText()))
            else :
                subent.append(str(self.Inst.avail.GetItem\
                    (self.Inst.avail.FindItem(0,self.Inst.selc.GetItemText\
                    (item)), 1).GetText()))
            subent.append(str(self.Inst.avail.GetItem\
                (self.Inst.avail.FindItem(0,self.Inst.selc.GetItemText\
                (item)), 3).GetText()).lower())
            if str(self.Inst.avail.GetItem\
                (self.Inst.avail.FindItem(0,self.Inst.selc.GetItemText\
                (item)), 3).GetText()) == 'METHOD':
                    subent.append(str(self._findclass\
                        (int(self.Inst.selc.GetItemText(item)))))

            selected_list.append(subent)

        if selected_list:
            return selected_list
        else:
            return None

    def _findclass(self,index):
        for i in range(index,0,-1):
            if self.Inst.avail.GetItem(i-1,3).GetText() == 'CLASS':
                return self.Inst.avail.GetItem(i-1,1).GetText()
            else:
                pass

    def _getavailable(self):

        selected_list=[]

        for item in range(self.Inst.avail.GetItemCount()):
            subent=[]
            if str(self.Inst.avail.GetItem(item, 3).GetText()) == 'METHOD':
                subent.append(str\
                    (self.Inst.avail.GetItem(item, 2).GetText()))
            else:
                subent.append(str\
                    (self.Inst.avail.GetItem(item, 1).GetText()))

            subent.append(str\
                (self.Inst.avail.GetItem(item, 3).GetText()).lower())

            if str(self.Inst.avail.GetItem(item, 3).GetText()) == 'METHOD':
                subent.append(str\
                    (self._findclass(int(self.Inst.avail.GetItemText(item)))))

            selected_list.append(subent)

        return selected_list
